//
//  ProductCollectionViewCell.swift
//  OnceNectar
//
//  Created by Zhang Dream on 18/06/2018.
//  Copyright © 2018 com.inwhoop. All rights reserved.
//

import UIKit
import SnapKit

class ProductCollectionViewCell: UICollectionViewCell, BottomShadow, AddCornerUIRectCorner {
    
    var iconImageView:UIImageView?
    var leftTopButton:UIButton?
    var footerView:UIView?
    
    var nameBrandLable:UILabel?
    
    var nameLable:UILabel?
    
    var contentLable:UILabel?
    
    var priceLable:UILabel?
    
    var dollaLable:UILabel?
    
    var model: GoodsModel?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.backgroundColor = UIColor.white
        let icon:UIImageView  = UIImageView(frame:.zero)
        self.contentView.addSubview(icon)
        self.iconImageView = icon
       
        self.iconImageView?.snp.makeConstraints { (make) in
            make.height.equalTo(20)
        }
      
        let leftTopButton:UIButton  = UIButton(type: .custom)
        leftTopButton.setBackgroundImage(UIImage(named: "sub_banner_text_bg"), for: .normal)
        leftTopButton.setTitle("为您推荐", for: .normal)
        leftTopButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        self.contentView.addSubview(leftTopButton)
        self.leftTopButton = leftTopButton
        self.leftTopButton?.isHidden = true
        
        let footer:UIView  = UIView(frame:.zero)
        self.contentView.addSubview(footer)
        self.footerView = footer
        
        let lable:UILabel  = UILabel()
        lable.font = UIFont.systemFont(ofSize: 12)
        lable.text = "品牌"
        lable.textAlignment = .left
        lable.frame = CGRect(x: 10,y: 5,width: self.frame.size.width - 20,height: 17)
        footer.addSubview(lable)
        self.nameBrandLable = lable
        
        let lable0:UILabel  = UILabel()
        lable0.font = UIFont.systemFont(ofSize: 14)
        lable0.text = "商品名"
        lable0.frame = CGRect(x: 10,y: lable.bottom + 5,width: self.frame.size.width - 20,height: 17)
        footer.addSubview(lable0)
        self.nameLable = lable0
        
        let lable1:UILabel  = UILabel()
        lable1.font = UIFont.systemFont(ofSize: 8)
        lable1.text = "具体参数"
        lable1.textAlignment = .left
        lable1.numberOfLines = 0
        lable1.lineBreakMode = .byWordWrapping
        lable1.frame = CGRect(x: 10,y: lable0.bottom + 3,width: self.frame.size.width - 20,height: 40)
        footer.addSubview(lable1)
        self.contentLable = lable1
        
        let lable3:UILabel  = UILabel()
        lable3.textColor = UIColor(red: 0 / 255.0, green: 160 / 255.0, blue: 230 / 255.0, alpha: 1);
        lable3.font = UIFont.systemFont(ofSize: 15)
        lable3.text = "666"
        footer.addSubview(lable3)
        self.priceLable = lable3
        
        let lable2:UILabel  = UILabel()
        lable2.font = UIFont.systemFont(ofSize: 15)
        lable2.text = "￥："
        
        footer.addSubview(lable2)
        self.dollaLable = lable2
       
       self.priceLable = lable3
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.iconImageView?.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView.snp.left)
            make.right.equalTo(self.contentView.snp.right);
            make.top.equalTo(self.contentView.snp.top);
        }
    
        self.leftTopButton?.snp.makeConstraints { (make) in
            make.left.equalTo(self.contentView.snp.left);
            make.top.equalTo(self.contentView.snp.top).offset(13);
            make.width.equalTo(64);
            make.height.equalTo(15);
        }
        
        var heig = 40
        if let goodsAlias = self.model?.goodsAlias{
            heig = Int(String.getAttributeStringHeight(attributeString: goodsAlias as NSString, maxWidth: self.width - 20, maxHeight: CGFloat(MAXFLOAT)))
            contentLable?.height = String.getAttributeStringHeight(attributeString: goodsAlias as NSString, maxWidth: self.width - 20, maxHeight: CGFloat(MAXFLOAT))
        }
        var hh: CGFloat = CGFloat(75 + heig)
        self.footerView?.frame = CGRect(x: 0,y: self.contentView.bottom - hh,width: self.contentView.frame.size.width,height: hh)
        
        self.priceLable?.bottom = (self.footerView?.height)! - 7;
        self.priceLable?.right = (self.footerView?.right)! - 18;
        self.priceLable?.sizeToFit()

        self.dollaLable?.right = (self.priceLable?.left)!
        self.dollaLable?.bottom = (self.priceLable?.bottom)!
        self.dollaLable?.sizeToFit()
        
        setShadow(view: self, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        addCornerWithView(aView: self.contentView, aCorners: .allCorners, aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
    }
    
    func bindData(model: GoodsModel) -> Void {
        self.model = model
        
        if !(model.coverPath?.isEmpty)! {
            let coverPath = model.coverPath?.replacingOccurrences(of: "\\", with: "")
            let path:String = "\(BaseURL)\(coverPath!)"
            
            let url:URL = URL(string:path)!
            iconImageView?.sd_setImage(with: url, completed: nil)
        
        }
        nameBrandLable?.text = model.label?.name!
        nameLable?.text = model.goodsName
        if let goodsAlias = model.goodsAlias{
            contentLable?.text = goodsAlias
        }
        if let count = model.specifications?.count, count > 0 {
          if let sper: SpecificationsModel = model.specifications?[0] {
              priceLable?.text = sper.price
          }
      }
        
        
        if (Int(model.coverHeight!) == 0 || Int(model.coverWidth!) == 0) {
            self.iconImageView?.snp.updateConstraints({ (make) in
                make.height.equalTo(240)
            })
//            self.iconImageView?.height = 240
            return
        }
        let height:CGFloat = CGFloat(Float(model.coverHeight!)! / Float(model.coverWidth!)!)
        self.iconImageView?.snp.updateConstraints({ (make) in
            make.height.equalTo(height * (ScreenWidth - 30) / 2)
        })
//        self.iconImageView?.height = height * (ScreenWidth - 30) / 2
    }
    
    func bindData2(model: GoodsSCModel) -> Void {
        
        if let showImage = model.image {
            let path:String = "\(BaseURL)\(showImage)"
            
            let url:URL = URL(string:path)!
            iconImageView?.sd_setImage(with: url, completed: nil)
            
        }
        nameLable?.text = model.title
        priceLable?.text = model.price
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    static func caculateHeight(model: GoodsModel) -> CGFloat{
        if (Int(model.coverHeight!) == 0 || Int(model.coverWidth!) == 0){
            var heig = 40
            if let goodsAlias = model.goodsAlias{
               
                heig = Int(String.getAttributeStringHeight(attributeString: goodsAlias as NSString, maxWidth: (ScreenWidth - 30) / 2 - 20, maxHeight: CGFloat(MAXFLOAT)))
            }
            return CGFloat(240 + 75 + heig)
        }
        var heig = 40
        if let goodsAlias = model.goodsAlias{
            
            heig = Int(String.getAttributeStringHeight(attributeString: goodsAlias as NSString, maxWidth: (ScreenWidth - 30) / 2 - 20, maxHeight: CGFloat(MAXFLOAT)))
        }
        var height:CGFloat = CGFloat(Float(model.coverHeight!)! / Float(model.coverWidth!)!)
        height = height * (ScreenWidth - 30) / 2 + 75
        height += CGFloat(heig)
        return height
    }
}
